#include "warningstatisticsinterface.h"
#include "ui_warningstatisticsinterface.h"
Q_GLOBAL_STATIC(WarningStatisticsInterface,theInstance);
WarningStatisticsInterface::WarningStatisticsInterface(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WarningStatisticsInterface)
{
    ui->setupUi(this);
    this->setWindowTitle("报警信息统计界面");
    this->setWindowIcon(QIcon(":res/res/AlarmStatistics.ico"));
    setLcdNumber();//设置统计显示格式

}

WarningStatisticsInterface::~WarningStatisticsInterface()
{
    delete ui;
}

WarningStatisticsInterface *WarningStatisticsInterface::instance()
{
    return theInstance;
}

void WarningStatisticsInterface::RefreshWarningTimes()
{
    //获取最新的报警次数值
     extern QVector<int>SensorWarningTimesVector;
     ui->lcdNumberWarning_AirT->display(SensorWarningTimesVector[0]);
     ui->lcdNumberWarning_AirH->display(SensorWarningTimesVector[1]);
     ui->lcdNumberWarning_Light->display(SensorWarningTimesVector[2]);
     ui->lcdNumberWarning_CO2->display(SensorWarningTimesVector[3]);
     ui->lcdNumberWarning_SoilH->display(SensorWarningTimesVector[4]);
     //刷新图表
   showColoredWarningBarChart();
}



void WarningStatisticsInterface::on_pushButtonExit_clicked()
{
  this->close();
}


void WarningStatisticsInterface::on_pushButtonRefresh_clicked()
{
    RefreshWarningTimes();
}

void WarningStatisticsInterface::setLcdNumber()
{
    QString style2="QLCDNumber {"
                   "   color: rgb(255, 0, 0);"          // 数字颜色
            "   background-color: black;" // 背景颜色
            "}";
    ui->lcdNumberWarning_AirT->setStyleSheet(style2);
    ui->lcdNumberWarning_AirT->setSegmentStyle(QLCDNumber::Flat); // 确保颜色生效

    ui->lcdNumberWarning_AirH->setStyleSheet(style2);
    ui->lcdNumberWarning_AirH->setSegmentStyle(QLCDNumber::Flat); // 确保颜色生效

    ui->lcdNumberWarning_Light->setStyleSheet(style2);
    ui->lcdNumberWarning_Light->setSegmentStyle(QLCDNumber::Flat); // 确保颜色生效

    ui->lcdNumberWarning_CO2->setStyleSheet(style2);
    ui->lcdNumberWarning_CO2->setSegmentStyle(QLCDNumber::Flat); // 确保颜色生效

    ui->lcdNumberWarning_SoilH->setStyleSheet(style2);
    ui->lcdNumberWarning_SoilH->setSegmentStyle(QLCDNumber::Flat); // 确保颜色生效
}

void WarningStatisticsInterface::showColoredWarningBarChart(QWidget *parent)
{
    // 每类报警次数
    //获取最新的报警次数值
       extern QVector<int>SensorWarningTimesVector;


       // 分别创建每个 QBarSet
       QBarSet *setTemp = new QBarSet("空气温度");
       *setTemp << SensorWarningTimesVector[0]<< 0 << 0 << 0 << 0;
       setTemp->setColor(Qt::red);

       QBarSet *setHumidity = new QBarSet("空气湿度");
       *setHumidity << 0<<SensorWarningTimesVector[1]<<0<<0<<0;
       setHumidity->setColor(Qt::blue);

       QBarSet *setLight = new QBarSet("光照强度");
       *setLight <<0<<0<<SensorWarningTimesVector[2]<<0<<0;
       setLight->setColor(Qt::yellow);

       QBarSet *setCO2 = new QBarSet("CO2浓度");
       *setCO2 <<0<<0<<0<< SensorWarningTimesVector[3]<<0;
       setCO2->setColor(Qt::green);

       QBarSet *setSoil = new QBarSet("土壤湿度");
       *setSoil << 0<<0<<0<<0<<SensorWarningTimesVector[4];
       setSoil->setColor(Qt::magenta);

       // 构建柱状图系列，每个 set 是一类
       QBarSeries *series = new QBarSeries();
       series->append(setTemp);
       series->append(setHumidity);
       series->append(setLight);
       series->append(setCO2);
       series->append(setSoil);
       series->setBarWidth(0.9);
       // 创建图表
       QChart *chart = new QChart();
       chart->addSeries(series);
       chart->setTitle("各传感器报警次数统计");
       chart->setAnimationOptions(QChart::SeriesAnimations);

       // x轴每类只显示一个柱子
       QStringList categories;
      // categories << "";  // 由于我们每个类别是独立 set，这里只需要一个 x 轴分组

         categories << "空气温度" << "空气湿度" << "光照强度" << "CO2浓度" << "土壤湿度";
       QBarCategoryAxis *axisX = new QBarCategoryAxis();
       axisX->append(categories);
       chart->addAxis(axisX, Qt::AlignBottom);
       series->attachAxis(axisX);

       // y轴
       QValueAxis *axisY = new QValueAxis();
       axisY->setTitleText("报警次数");
       chart->addAxis(axisY, Qt::AlignLeft);
       series->attachAxis(axisY);

       // 显示图表视图
       ui->chartView->setChart(chart);
//       QChartView *chartView = new QChartView(chart, parent);
       ui->chartView->setRenderHint(QPainter::Antialiasing);
//       chartView->resize(640, 400);
//       chartView->show();
}

